#include "ScriptActionLauncher.h"
#include "ui_ScriptActionLauncher.h"

#include "ApplicationIcon.h"
#include "qmessagebox.h"

ScriptActionLauncher::ScriptActionLauncher( ScriptActionTrigger & actionTrigger, QWidget *parent) :
   QWidget(parent),
   m_actionTrigger(actionTrigger),
   ui(new Ui::ScriptActionLauncher),
   m_type(invalidAction)
{
   ui->setupUi(this);

   m_lightPalette.setBrush(QPalette::Window, QColor( 200, 200, 20, 160) );
   m_mediaPalette.setBrush(QPalette::Window, QColor( 0, 0, 200, 160) );
   setAutoFillBackground(true);
   ui->playButton->setIcon( QIcon(IconPath("track_play.png")));
}

ScriptActionLauncher::~ScriptActionLauncher()
{
   delete ui;
}

void ScriptActionLauncher::setActionName(const QString &name)
{
   ui->actionName->setText( name );
}

void ScriptActionLauncher::setActionType( ActionType type)
{
   m_type = type;

   if (type == audVidAction)
   {
      ui->iconLabel->setPixmap(QPixmap(IconPath("sound.png")));
      ui->activateButton->setEnabled(true);
      setPalette( m_mediaPalette);
      ui->actionName->setStyleSheet( "QLabel { color: yellow; background-color: cyan; }" );
   }
   else if (type == lightAction)
   {
      ui->iconLabel->setPixmap(QPixmap(IconPath("light.png")));
      ui->activateButton->setEnabled(true);
      setPalette( m_lightPalette);
      ui->actionName->setStyleSheet( "QLabel { color: purple; background-color: orange; }" );
   }
   else if (type == pictureAction)
   {
      ui->iconLabel->setPixmap(QPixmap(IconPath("picture.png")));
      ui->activateButton->setEnabled(true);
      setPalette( m_mediaPalette);
      ui->actionName->setStyleSheet( "QLabel { color: yellow; background-color: cyan; }" );
   }
   else
   {
      setPalette( QPalette());
      ui->iconLabel->setPixmap(QPixmap());
      ui->activateButton->setEnabled(false);
      ui->actionName->setStyleSheet("");
   }
}

/**
 * activate button callback
 */
void ScriptActionLauncher::on_activateButton_clicked()
{
   tryToActivate( false);
}

/**
 * play button callback
 */
void ScriptActionLauncher::on_playButton_clicked()
{
   tryToActivate( true);
}

void ScriptActionLauncher::tryToActivate(bool playImmediately)
{
   try
   {
      activateCurrentAction( playImmediately);
   }
   catch( QString & err)
   {
      QMessageBox::warning( this, tr("Error executin script action"),
                            tr("Error executing %1 action:\n  %2").
                            arg((m_type == audVidAction) ? "media" : "light").
                            arg(err) );
   }
}

/**
 * emit a signal to let system activate an action
 */
void ScriptActionLauncher::activateCurrentAction(bool playImmediately)
{
   if (m_type == audVidAction)
   {
      emit m_actionTrigger.activateMedia( ui->actionName->text(), playImmediately );
   }
   else if (m_type == lightAction)
   {
      emit m_actionTrigger.activateLight( ui->actionName->text(), playImmediately );
   }
   else if (m_type == pictureAction)
   {
      emit m_actionTrigger.activatePicture( ui->actionName->text(), playImmediately );
   }
}
